package com.zam.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zam.pojo.UserExtend;
import com.zam.service.IUserExtendService;
import com.zam.service.UserService;
import com.zam.service.impl.UserExtendServiceImpl;
import org.apache.hadoop.yarn.webapp.hamlet2.Hamlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author talent_dog
 * @Date 2024/2/13 19:36
 * @Version 1.0
 */
@Component
public class IntegralUtil {

    private static IUserExtendService staticUserExtendService;

    @Resource
    private IUserExtendService userExtendService;

    @PostConstruct
    public void setUserExtendService() {
        staticUserExtendService = this.userExtendService;
    }


    //正常签到
//    @Transactional
    public static void normalCheckin(Integer userId) {
        //当日签到，积分+5
        UserExtend userExtend = staticUserExtendService.getOne(new LambdaQueryWrapper<UserExtend>().eq(UserExtend::getUserId,userId));
        LambdaUpdateWrapper<UserExtend> LUW= new LambdaUpdateWrapper<>();
        LUW.eq(UserExtend::getUserId,userId);
        LUW.set(UserExtend::getIntegral, userExtend.getIntegral() + 5);
        staticUserExtendService.update(LUW);
    }

    //补签
//    @Transactional
    public static void afterCheckin(Integer userId) {
        //补签，积分+2
        UserExtend userExtend = staticUserExtendService.getOne(new LambdaQueryWrapper<UserExtend>().eq(UserExtend::getUserId,userId));
        LambdaUpdateWrapper<UserExtend> LUW= new LambdaUpdateWrapper<>();
        LUW.eq(UserExtend::getUserId,userId).set(UserExtend::getIntegral, userExtend.getIntegral() + 2);
        staticUserExtendService.update(LUW);

    }

    //使用积分
//    @Transactional
    public static Boolean useIntegral(Integer userId) {
        //使用积分，积分-10
        UserExtend userExtend = staticUserExtendService.getOne(new LambdaQueryWrapper<UserExtend>().eq(UserExtend::getUserId,userId));
        if (userExtend.getIntegral() < 20) {
            return false;
        }
        LambdaUpdateWrapper<UserExtend> LUW= new LambdaUpdateWrapper<>();
        LUW.eq(UserExtend::getUserId,userId);
        LUW.set(UserExtend::getIntegral, userExtend.getIntegral() - 20);
        staticUserExtendService.update(LUW);
        return true;
    }

    //被收藏，积分+10
//    @Transactional
    public static void collectIntegral(Integer userId) {
        //使用积分，积分-10
        UserExtend userExtend = staticUserExtendService.getOne(new LambdaQueryWrapper<UserExtend>().eq(UserExtend::getUserId,userId));
        LambdaUpdateWrapper<UserExtend> LUW= new LambdaUpdateWrapper<>();
        LUW.eq(UserExtend::getUserId,userId);
        LUW.set(UserExtend::getIntegral, userExtend.getIntegral() + 10);
        staticUserExtendService.update(LUW);
    }



}
